package yicheng.dinnertogetherapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Message;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ContactListActivity extends Activity{

	private ListView contact_list_listView;

	private ArrayList<String> contact_name_list = new ArrayList<String>();
	private ArrayList<String> contact_number_list = new ArrayList<String>();
	
	private List<String> domainNames = new ArrayList<String>(StartingViewActivity.domainNames);
	

	public static SharedPreferences local_user_information;
	private SharedPreferences.Editor local_user_editor;
	private String PREFS_NAME = "LocalUserInfo";

	private String PHONE_NUMBER;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.contact_list_layout);
		overridePendingTransition(R.anim.activity_left_in,R.anim.activity_left_out);

		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		PHONE_NUMBER = local_user_information.getString("PhoneNumber", "default");

		getContactInfo();

		setListViewControl();


	}

	private void getContactInfo(){
		Cursor cursor = getContentResolver()
				.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

		while (cursor.moveToNext()) {

			String contact_phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			contact_number_list.add(contact_phoneNumber);

			String contact_name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


			if (!contact_phoneNumber.equals(PHONE_NUMBER)){
				if (domainNames.contains(contact_phoneNumber)){
					contact_name += "     online";
					contact_name_list.add(contact_name);
				}
				else{
					contact_name += "     offline";
					contact_name_list.add(contact_name);
				}
			}
			





		}





	}

	private void setListViewControl(){

		contact_list_listView = (ListView) findViewById(R.id.contact_list_listView);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(ContactListActivity.this,
				R.layout.list_view_layout, R.id.listView_item_textView, contact_name_list);
		contact_list_listView.setAdapter(adapter);


		contact_list_listView.setOnItemClickListener(new ListView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Bundle data_bundle = new Bundle();
				data_bundle.putString("contact_info_name", contact_name_list.get(position));

				data_bundle.putString("contact_info_number", contact_number_list.get(position));
				
				if (domainNames.contains(contact_number_list.get(position))){
					
					data_bundle.putBoolean("is_using_app", true);
				}
				else{

					data_bundle.putBoolean("is_using_app", false);
				}
				
				Intent gotoContactInfoActivity = new Intent("yicheng.dinnertogetherapp.CONTACTINFOACTIVITY");
				gotoContactInfoActivity.putExtra("contactInfo", data_bundle);
				startActivity(gotoContactInfoActivity);
				
			}

		});
	}



	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.activity_right_in, R.anim.activity_right_out);
	}





}
